package com.egg.mall.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.OptionsResponse;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.common.util.OptionalUtil;
import com.egg.mall.entity.category.Category;
import com.egg.mall.entity.category.CategoryAttribute;
import com.egg.mall.model.request.AttributeListPut;
import com.egg.mall.model.request.CategoryPostOrPut;
import com.egg.mall.model.response.web.AttributeList;
import com.egg.mall.model.response.web.CategoryInfo;
import com.egg.mall.model.response.web.CategoryTree;
import com.egg.mall.service.category.ICategoryAttributeService;
import com.egg.mall.service.category.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author east
 * @since 2022-08-08
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "商品分类")
@RestController
@RequestMapping("/category")
public class WebCategoryController extends WebController {


    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ICategoryAttributeService categoryAttributeService;



    @ApiOperation(value = "商品分类新增")
    @PostMapping
    public ResponseData<Long> categoryPost(@RequestBody CategoryPostOrPut param) {
        Long id = categoryService.add(param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "商品分类树")
    @GetMapping("/tree")
    public ResponseData<List<CategoryTree>> categoryTreeGet() {
        List<CategoryTree> tree = categoryService.tree();
        return ResponseData.success(tree);
    }

    @ApiOperation(value = "商品分类详情")
    @GetMapping("/{id}/info")
    public ResponseData<CategoryInfo> categoryInfoGet(
            @ApiParam(value = "商品分类主键ID", required = true) @PathVariable String id
    ) {
        Category dbInfo = categoryService.baseGetById(id);
        CategoryInfo info = JsonUtil.objToObj(dbInfo, CategoryInfo.class);
        return ResponseData.success(info);
    }

    @ApiOperation(value = "商品分类编辑")
    @PutMapping("/{id}")
    public ResponseData<Long> categoryPut(
            @ApiParam(value = "商品分类主键ID", required = true) @PathVariable String id
            , @RequestBody CategoryPostOrPut edit
    ) {
        Long existId = categoryService.edit(id, edit);
        return ResponseData.success(existId);
    }

    @ApiOperation(value = "商品分类删除")
    @DeleteMapping
    public ResponseData<String> categoryDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        categoryService.delCategory(
                ids.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }

    @ApiOperation(value = "商品分类下拉选")
    @GetMapping("/option")
    public ResponseData<List<OptionsResponse>> deptOptionGet() {
        List<CategoryTree> tree = categoryService.tree();
        List<OptionsResponse> list = deptTreeChangeOption(tree);
        return ResponseData.success(list);
    }

    private List<OptionsResponse> deptTreeChangeOption(List<CategoryTree> tree) {
        if (ObjectUtil.isEmpty(tree)) {
            return Collections.emptyList();
        }
        List<OptionsResponse> optionList = new ArrayList<>();
        tree.forEach(o -> {
            OptionsResponse option = new OptionsResponse();
            option.setLabel(o.getName());
            option.setValue(o.getId().toString());
            option.setChildren(deptTreeChangeOption(o.getChildren()));
            optionList.add(option);
        });
        return optionList;
    }


    @ApiOperation(value = "商品分类属性覆盖保存")
    @PutMapping("/{id}/attribute/list")
    public ResponseData<List<Long>> attributeListPut(
            @ApiParam(value = "分类ID", required = true) @PathVariable(value = "id") String id
            , @RequestBody AttributeListPut param
    ) {
        List<Long> ids = categoryAttributeService.put(id, param);
        return ResponseData.success(ids);
    }

    @ApiOperation(value = "商品分类属性分页")
    @GetMapping("/{id}/attribute/list")
    public ResponseData<List<AttributeList>> attributeListGet(
            @ApiParam(value = "分类ID", required = true) @PathVariable(value = "id") String id
            , @ApiParam(value = "属性类型") @RequestParam(value = "type", required = false) String type
    ) {
        List<CategoryAttribute> dbList = categoryAttributeService.find(
                OptionalUtil.build(Arrays.asList(id))
                , OptionalUtil.build(type)
        );
        List<AttributeList> list = JsonUtil.listToList(dbList, AttributeList.class);
        return ResponseData.success(list);
    }
}

